﻿namespace YidanSoft.Library.EditorUtility.General
{
    using System;
    using System.Drawing;
    using YidanSoft.Library.EditorUtility.Win32;

    public class GDIUtil
    {
        private GDIUtil()
        {
        }

        public static void Draw3DRect(Graphics g, Rectangle rc, Color topLeft, Color bottomRight)
        {
            Draw3DRect(g, rc.Left, rc.Top, rc.Width, rc.Height, topLeft, bottomRight);
        }

        public static void Draw3DRect(Graphics g, int x, int y, int width, int height, Color topLeft, Color bottomRight)
        {
            g.FillRectangle(new SolidBrush(topLeft), x, y, width - 1, 1);
            g.FillRectangle(new SolidBrush(topLeft), x, y, 1, height - 1);
            g.FillRectangle(new SolidBrush(bottomRight), x + width, y, -1, height);
            g.FillRectangle(new SolidBrush(bottomRight), x, y + height, width, -1);
        }

        public static void DrawArrowGlyph(Graphics g, Rectangle rc, bool up, Brush brush)
        {
            DrawArrowGlyph(g, rc, 5, 3, up, brush);
        }

        public static void DrawArrowGlyph(Graphics g, Rectangle rc, int arrowWidth, int arrowHeight, bool up, Brush brush)
        {
            Point[] points = new Point[3];
            int y = ((rc.Top + (rc.Height / 2)) - (arrowHeight / 2)) + 1;
            int x = rc.Left + (rc.Width / 2);
            if (up)
            {
                points[0] = new Point(x, y - 2);
                points[1] = new Point((x - (arrowWidth / 2)) - 1, (y + arrowHeight) - 1);
                points[2] = new Point((x + (arrowWidth / 2)) + 1, (y + arrowHeight) - 1);
            }
            else
            {
                points[0] = new Point(x - (arrowWidth / 2), y);
                points[1] = new Point((x + (arrowWidth / 2)) + 1, y);
                points[2] = new Point(x, y + arrowHeight);
            }
            g.FillPolygon(brush, points);
        }

        public static Bitmap GetStrechedBitmap(Graphics gDest, Rectangle rcDest, Bitmap bitmap)
        {
            Bitmap image = new Bitmap(rcDest.Width, rcDest.Height);
            Graphics graphics = Graphics.FromImage(image);
            IntPtr hdc = graphics.GetHdc();
            WindowsAPI.SetStretchBltMode(hdc, 3);
            IntPtr hDC = WindowsAPI.CreateCompatibleDC(hdc);
            IntPtr hObject = WindowsAPI.SelectObject(hDC, bitmap.GetHbitmap());
            WindowsAPI.StretchBlt(hdc, rcDest.Left, rcDest.Top, rcDest.Width, rcDest.Height, hDC, 0, 0, bitmap.Width, bitmap.Height, 0xcc0020);
            WindowsAPI.SelectObject(hDC, hObject);
            graphics.ReleaseHdc(hdc);
            return image;
        }

        public static Bitmap GetTileBitmap(Rectangle rcDest, Bitmap bitmap)
        {
            Bitmap image = new Bitmap(rcDest.Width, rcDest.Height);
            using (Graphics graphics = Graphics.FromImage(image))
            {
                for (int i = 0; i < image.Width; i += bitmap.Width)
                {
                    for (int j = 0; j < image.Height; j += bitmap.Height)
                    {
                        graphics.DrawImage(bitmap, new Point(i, j));
                    }
                }
            }
            return image;
        }

        public static void StrechBitmap(Graphics gDest, Rectangle rcDest, Bitmap bitmap)
        {
            IntPtr hdc = gDest.GetHdc();
            WindowsAPI.SetStretchBltMode(hdc, 4);
            IntPtr hDC = WindowsAPI.CreateCompatibleDC(hdc);
            IntPtr hObject = WindowsAPI.SelectObject(hDC, bitmap.GetHbitmap());
            WindowsAPI.StretchBlt(hdc, rcDest.Left, rcDest.Top, rcDest.Width, rcDest.Height, hDC, 0, 0, bitmap.Width, bitmap.Height, 0xcc0020);
            WindowsAPI.SelectObject(hDC, hObject);
            gDest.ReleaseHdc(hdc);
        }

        public static void StrechBitmap(Graphics gDest, Rectangle rcDest, Rectangle rcSource, Bitmap bitmap)
        {
            IntPtr hdc = gDest.GetHdc();
            WindowsAPI.SetStretchBltMode(hdc, 3);
            IntPtr hDC = WindowsAPI.CreateCompatibleDC(hdc);
            IntPtr hObject = WindowsAPI.SelectObject(hDC, bitmap.GetHbitmap());
            WindowsAPI.StretchBlt(hdc, rcDest.Left, rcDest.Top, rcDest.Width, rcDest.Height, hDC, rcSource.Left, rcSource.Top, rcSource.Width, rcSource.Height, 0xcc0020);
            WindowsAPI.SelectObject(hDC, hObject);
            gDest.ReleaseHdc(hdc);
        }
    }
}

